﻿using ReflectionStudio.Core.Events;
using System;
using System.Windows;
namespace WPFExcelReport
{

    public partial class ITXUPDATE
    {
        #region ==INFO===========
        public static readonly DependencyProperty INFOProperty = DependencyProperty.Register("INFO",
            typeof(string), typeof(ITXUPDATE),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnINFOChanged));
        public string INFO
        {
            get
            {
                return (string)GetValue(INFOProperty);
            }
            set
            {
                SetValue(INFOProperty, value);
            
            }
        }
        private static void OnINFOChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as ITXUPDATE;
            if (src != null)
            {
                src.INFO = (string)e.NewValue;
                src.OnINFOChg((string)e.NewValue);
            }
        }
        public virtual void OnINFOChg(string value)
        {
            
            notifyIcon.BalloonTipText = value;
            if (value.Contains("有更新"))
            {
                notifyIcon.ShowBalloonTip(2000);
            } 
        }
        #endregion
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }

        public ITXUPDATE()
        {
            app.Load();
            InitializeComponent();
            this.Name = "自动更新";
            DataContext = this;
            cUp.ART = "D:/" + app.company + "Prod";
            cUp.VAR = "5";
            InitialTray(); 
            this.Visibility = Visibility.Hidden; 
        }
        private System.Windows.Forms.NotifyIcon notifyIcon = null;
        private void InitialTray()
        {
            try
            { 
                //设置托盘的各个属性
                notifyIcon = new System.Windows.Forms.NotifyIcon();
                //notifyIcon.BalloonTipText = "没有更新";
                notifyIcon.Text = "自动更新";
                notifyIcon.Icon = new System.Drawing.Icon(System.Windows.Forms.Application.StartupPath + "\\intex.ico");
                notifyIcon.Visible = true;
                //notifyIcon.ShowBalloonTip(20);
                notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick); 

                //退出菜单项
                System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("退出");
                exit.Click += new EventHandler(exit_Click);
                System.Windows.Forms.MenuItem chknew = new System.Windows.Forms.MenuItem("检查更新");
                chknew.Click += new EventHandler(chknew_Click);
                System.Windows.Forms.MenuItem menu = new System.Windows.Forms.MenuItem("菜单");
                //关联托盘控件
                System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { menu, exit,chknew};
                notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);
                //窗体状态改变时候触发
                this.StateChanged += new EventHandler(SysTray_StateChanged);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
        private void SysTray_StateChanged(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Minimized)
            {
                this.Visibility = Visibility.Hidden;
            }
        }
        private void chknew_Click(object sender, EventArgs e)
        {
            cUp.TMV.CHKUPDATE();
        }
        private void exit_Click(object sender, EventArgs e)
        {
            if (System.Windows.MessageBox.Show("确定要关闭吗?",
                                               "退出",
                                                MessageBoxButton.YesNo,
                                                MessageBoxImage.Question,
                                                MessageBoxResult.No) == MessageBoxResult.Yes)
            {
                notifyIcon.Dispose();
                System.Windows.Application.Current.Shutdown();
            }
        }
        private void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                if (this.Visibility == Visibility.Visible)
                {
                    this.Visibility = Visibility.Hidden;
                }
                else
                {
                    this.Visibility = Visibility.Visible;
                    this.Activate();
                }
            }
        }


    }
}
